const api = require('../../../api/store_api');
import { toast, log, err } from '../../../utils/wxapi';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    size: 10,
    list: [],
    paylist: [],
    pageTotal: 0,
    nav: [
      { name: '全部', status: '' },
      { name: '待审核', status: 'AWAITING_MODERATION' },
      { name: '审核通过', status: 'PASS_AUDIT' },
      { name: '售后完成', status: 'RETURN_SUCCESS' },
      { name: '售后关闭', status: 'CLOSE' },
    ],
    navnum: 0,
  },
  onLoad() {
    this.pay_return_list();
  },
  navtap(e) {
    try {
      const i = e.currentTarget.dataset.index;
      const resetList = navnum => {
        this.setData({ navnum, list: [], page: 1 });
      };
      resetList(i === 0 ? i : i);
      i === 0 ? this.pay_return_list() : this.status_list(this.data.nav[i].status);
    } catch (error) {
      err(error);
      throw error;
    }
  },
  async status_list(status) {
    try {
      const { page, size } = this.data;
      const data = { status };
      const res = await api.payrefundList({ page, size, data });
      const list = JSON.parse(res.source);
      const pageTotal = res.pageTotal;
      console.log(list);
      let updatedOdlist;
      if (this.data.list.length === 0) {
        updatedOdlist = list;
      } else {
        updatedOdlist = list.concat(list);
      }
      this.setData({
        list: updatedOdlist,
        pageTotal,
      });
    } catch (error) {
      err(error);
    }
  },
  async pay_return_list() {
    try {
      const { page, size } = this.data;
      const res = await api.payrefundList({ page, size });
      if (res.respCode === 200) {
        const list = JSON.parse(res.source);
        const pageTotal = res.pageTotal;
        log(list);
        let updatedOdlist;
        if (this.data.list.length === 0) {
          updatedOdlist = list;
        } else {
          updatedOdlist = list.concat(list);
        }
        this.setData({
          list: updatedOdlist,
          pageTotal,
        });
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let { list, pageTotal, page } = this.data;
    if (list.length < pageTotal) {
      this.setData({ page: page + 1 });
      this.pay_return_list();
    } else {
      toast('已加载全部数据');
    }
  },
});
